<script type="text/javascript">
    var result;
</script>
<?php
if (!empty($result)) {
    echo '<script type="text/javascript">'
    . 'result = ' . json_encode($result) . ";"
    . '</script>';
}
?>
<script type="text/javascript">
    if (result) {
        var html = '<div class="alert ' + (result["status"] == "0" ? "alert-success" : "alert-danger") + '">';
        html += '<button type="button" class="close" data-dismiss="alert">×</button>';
        html += result["message"];
        html += "</div>";
        $(".main-content .page-content .page-content-area").find("div").first().find("div").first().prepend(html);
    }
</script>

<script type="text/javascript">
    var fields;
</script>
<?php
if(isset($form))
{
	$fields = $form->getFieldValue();
}
if (!empty($fields)) {
    echo '<script type="text/javascript">'
    . 'fields = ' . json_encode($fields) . ";"
    . '</script>';
}
?>
<script type="text/javascript">
    if (fields) {
        for (var field in fields) {
            $("[name='" + field + "']").val(fields[field]);
            if ($("[name='" + field + "']").length > 0) {
                if ($("[name='" + field + "']")[0].tagName == "SELECT") {
                    $("[name='" + field + "']").attr("data", fields[field]);
                }
            }
        }
    }
</script>

<script type="text/javascript">
    var formErrorMessages;
</script>
<?php
if(isset($form))
{
	$formErrorMessages = $form->getMessages();
}
if (!empty($formErrorMessages)) {
    echo '<script type="text/javascript">'
    . 'formErrorMessages = ' . json_encode($formErrorMessages) . ";"
    . '</script>';
}
?>
<script type="text/javascript">
    if (formErrorMessages) {
        for (var field in formErrorMessages) {
            $("#" + field).addClass(" has-error");
            $("#" + field).append('<div class="help-block col-xs-12 col-sm-reset inline">' + formErrorMessages[field] + '</div>');
        }
    }
</script>